#!/bin/bash
export APPDIR="$(dirname "$(readlink -f "$0")")"
export PATH="$APPDIR/usr/bin/:$PATH"
export LD_LIBRARY_PATH="$APPDIR/usr/lib:$PATH"
export XDG_DATA_DIRS="$APPDIR/usr/share/:/usr/share/:$XDG_DATA_DIRS"
export DESKTOP_FILE_PATH=$APPDIR/usr/share/applications/net.winegame.bottles.desktop
export ICON_FILE_PATH=$APPDIR/usr/share/icons/hicolor/scalable/apps/net.winegame.bottles.svg
export SYMBOLIC_ICON_FILE_PATH=$APPDIR/usr/share/icons/symbolic/scalable/apps/net.winegame.bottles-symbolic.svg
export INSTALLED_DESKTOP_FILE_PATH=~/.local/share/applications/bottles.desktop
export INSTALLED_ICON_FILE_PATH=~/.local/share/icons/hicolor/scalable/apps/net.winegame.bottles.svg
export INSTALLED_SYMBOLIC_ICON_FILE_PATH=~/.local/share/icons/hicolor/symbolic/apps/net.winegame.bottles-symbolic.svg
export G_MESSAGES_DEBUG="fatal"
INSTALL_DESKTOP_FILE=false

if ! test -f $INSTALLED_ICON_FILE_PATH; then
	mkdir -p ~/.local/share/icons/hicolor/scalable/apps/
	cp $ICON_FILE_PATH $INSTALLED_SYMBOLIC_ICON_FILE_PATH
fi

if ! test -f $INSTALLED_SYMBOLIC_ICON_FILE_PATH; then
	mkdir -p ~/.local/share/icons/hicolor/symbolic/apps/
	cp $ICON_FILE_PATH $INSTALLED_SYMBOLIC_ICON_FILE_PATH
fi

if ! test -f $INSTALLED_DESKTOP_FILE_PATH; then
	if command -v zenity &> /dev/null
	then
		zenity --question --text="Do you want to install this AppImage?" --title "Bottles Installation"
		if [ $? = 0 ]; then
			INSTALL_DESKTOP_FILE=true
		fi
	elif command -v kdialog &> /dev/null
	then
		kdialog --yesno "Do you want to install this AppImage?" --yes-label "Install" --no-label "Just run" --title "Bottles Installation"
		if [ $? = 0 ]; then
			INSTALL_DESKTOP_FILE=true
		fi
	fi

	if $INSTALL_DESKTOP_FILE
	then
		cp $DESKTOP_FILE_PATH $INSTALLED_DESKTOP_FILE_PATH
		sed -i -e 's|Exec=bottles|Exec='$APPIMAGE' %F|g' $INSTALLED_DESKTOP_FILE_PATH
	fi
fi

if ! command -v python3 &> /dev/null
then
    python $APPDIR/usr/bin/bottles $1
else
    python3 $APPDIR/usr/bin/bottles $1
fi
